/*** 
 * @_______________#########_______________________ 
 * @______________############_____________________ 
 * @______________#############____________________ 
 * @_____________##__###########___________________ 
 * @____________###__######_#####__________________ 
 * @____________###_#######___####_________________ 
 * @___________###__##########_####________________ 
 * @__________####__###########_####_______________ 
 * @________#####___###########__#####_____________ 
 * @_______######___###_########___#####___________ 
 * @_______#####___###___########___######_________ 
 * @______######___###__###########___######_______ 
 * @_____######___####_##############__######______ 
 * @____#######__#####################_#######_____ 
 * @____#######__##############################____ 
 * @___#######__######_#################_#######___ 
 * @___#######__######_######_#########___######___ 
 * @___#######____##__######___######_____######___ 
 * @___#######________######____#####_____#####____ 
 * @____######________#####_____#####_____####_____ 
 * @_____#####________####______#####_____###______ 
 * @______#####______;###________###______#________ 
 * @________##_______####________####______________ 
 * @
 * @Author: ipk518 18163976442@163.com
 * @Date: 2024-06-19 10:31:14
 * @LastEditors: ipk518 121206530@qq.com
 * @LastEditTime: 2025-04-15 16:58:46
 * @FilePath: /etws-sxk2307/src/packetResolver/sdsCmdV1.cpp
 * @Description: 
 * @************佛祖保佑************
 * @Copyright (c) 2024 by etws@quyujiang, All Rights Reserved. 
 */

#include <string>
#include <iostream>
#include "lxCmdV1.h"
#include "sdsCmdV1.h"
#include "lxNodeV1.h"
#include "Server.h"
#include "Common.h"


using namespace std;

std::map<int32_t, CallbackCmdFun> queryCallbackMap = {
    {0x00020000,query_0_callback},
    {0x00020001,query_1_callback},
    {0x00020002,query_2_callback},
    {0x00020003,query_3_callback},
    {0x00020004,query_4_callback},
    {0x00020005,query_5_callback},
    {0x00020006,query_6_callback},
    {0x00020007,query_7_callback},
    {0x00020008,query_8_callback},
    {0x00020009,query_9_callback},
};

std::map<int32_t, CallbackCmdFun> sdsCallbackMap = {
    {servoHAngle,   sxkMotor_callback_0},
    {servoHSpeed,   sxkMotor_callback_1},
    {servoHCycle,   sxkMotor_callback_2},
    {servoHCW,      sxkMotor_callback_3},
    {servoHStop,    sxkMotor_callback_4},
    {servoHToZero,  sxkMotor_callback_5},
    {servoGetStr,   sxkMotor_callback_6},
    {servoHCali0,   sxkMotor_callback_7},
    {servoHCCW,     sxkMotor_callback_8},
    {servoHScan,    sxkMotor_callback_9},
    {servoVAngle,   sxkMotor_callback_10},
    {servoVSpeed,   sxkMotor_callback_12},
    {servoVStop,    sxkMotor_callback_13},
    {servoVCali30,  sxkMotor_callback_14},
    {servoVTo30,    sxkMotor_callback_15},
    {servoVCycle,   sxkMotor_callback_11},
    {servoHAccel,   sxkMotor_callback_21},
    {servoVAccel,   sxkMotor_callback_22},
    
    {0x00020000,sxk_callback_0},
    {0x00020001,sxk_callback_1},
    {0x00020002,sxk_callback_2},
    {0x00020003,sxk_callback_3},
    {0x00020004,sxk_callback_4},
    {0x00020005,sxk_callback_5},
    {0x00020006,sxk_callback_6},
    {0x00020007,sxk_callback_7},
    {0x00020008,sxk_callback_8},
    {0x0002000a,sxk_callback_10},
    {0x0002000b,sxk_callback_11},
    {0x0002000c,sxk_callback_12},
    {0x0002000d,sxk_callback_13},
    {0x0002000e,sxk_callback_14},
    {0x0002000f,sxk_callback_15},
    {0x00020010,sxk_callback_16},
    {0x00020011,sxk_callback_17},
    {0x00020012,sxk_callback_18},
    {0x00020013,sxk_callback_19},
    {0x00020014,sxk_callback_20},
    {0x00020015,sxk_callback_21},
    {0x00020016,sxk_callback_22},
    {0x00020017,sxk_callback_23},
    {0x00020018,sxk_callback_24},
    {0x00020019,sxk_callback_25},
    {0x0002001a,sxk_callback_26},
    {0x0002001b,sxk_callback_27},
    {0x0002001c,sxk_callback_28},
    {0x0002001d,sxk_callback_29},
    {0x0002001e,sxk_callback_30},
    {0x0002001f,sxk_callback_31},
    {0x00020020,sxk_callback_32},
    {0x00020021,sxk_callback_33},
    {0x00020022,sxk_callback_34},
    {0x00020023,sxk_callback_35},
    {0x00020024,sxk_callback_36},

    {0x00080000,global_callback_0},
    {0x00080001,global_callback_1},
    {0x00080002,global_callback_2},
    {0x00080003,global_callback_3},
    {0x00080004,global_callback_4},
    {0x00080005,global_callback_5},
    {0x00080006,global_callback_6},
    {0x00080007,global_callback_7},
    {0x00080008,global_callback_8},
};



void sdsProtocolAnalysis(struct cmdDataType* cmd,uint8_t * cmdData)
{
    // 解析指令
    int32_t cmdId = (((uint32_t)cmd->cmdType << 16) | cmd->cmdChildType);
    if ((cmd->dst_DevType == CurDeviceType) || (cmd->dst_DevType == allDevice)) {
        if (((cmd->dst_DevNum == ComponentNO) || (cmd->dst_DevNum == allDevice))) {
            ;
        } else {
            qlog_e("命令中的主指令:%d,子指令:%d",cmd->cmdType,cmd->cmdChildType);
            qlog_e("设备类型:%d,设备编号:%d,与本设备不符",cmd->dst_DevType,cmd->dst_DevNum);
            return;
        }
    } else {
        return;
    }

    qlog_i("接收到小上位机命令.");
    qlog_hexdump("sds cmd",16,(uint8_t *)cmd,sizeof(struct cmdDataType));
    qlog_hexdump("sds data",16,cmdData,cmd->dataLen);
    if(cmd->DeviceType == t_serverReplyCmd) {
        // 解析数据
        auto it = sdsCallbackMap.find(cmdId);
        if (it != sdsCallbackMap.end()) {
            it->second(cmd, cmdData);
        } else {
            qlog_e("这个SDS指令未实现..");
        }
    } else if(cmd->DeviceType == t_query) {
        // 解析数据
        auto it = queryCallbackMap.find(cmdId);
        if (it != queryCallbackMap.end()) {
            it->second(cmd, cmdData);
        } else {
            qlog_e("这个查询指令未实现..");
        }
    }
    
}
